package com.fanilycar51.mvp.common;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.ButterKnife;

/**
 * Created by lenovo on 2016/9/7.
 */
public abstract class CommonFragment extends Fragment {

    private View rootView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = View.inflate(getContext(), getLayoutId(), null);
        ButterKnife.bind(CommonFragment.this, rootView);
        //注册EventBus
        EventBus.getDefault().register(this);
        init(rootView, savedInstanceState);
        return rootView;
    }

    /**
     * 返回布局ID的方法
     *
     * @return
     */
    protected abstract int getLayoutId();

    /**
     * 初始化方法
     */
    protected abstract void init(View view,Bundle savedInstanceState);


    /**
     * EventBus的接受事件的方法
     *
     * @param busInfo
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void busEvent(BusInfo busInfo) {

    }

    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

}
